package com.ztrue.framework.core.event;

import java.util.Collection;

/**
 * 领域事件流
 *
 * @author zhangw
 * @date 2016/12/1 17:52
 */
public class DomainEventStream {
    /**
     * 命令ID
     */
    private String commandId;
    /**
     * 聚合根ID
     */
    private String aggregateRootId;
    /**
     * 聚合根类型名称
     */
    private String aggregateRootTypeName;
    /**
     * 聚合根版本
     */
    private Long version;
    /**
     * 领域事件集合
     */
    private Collection<IDomainEvent> domainEvents;

    /**
     * 初始化领域时间流
     *
     * @param commandId             命令ID
     * @param aggregateRootId       聚合根ID
     * @param aggregateRootTypeName 聚合根类型名称
     * @param version               聚合根最新版本
     * @param domainEvents          领域事件集合
     */
    public DomainEventStream(String commandId, String aggregateRootId, String aggregateRootTypeName, Long version, Collection<IDomainEvent> domainEvents) {
        this.commandId = commandId;
        this.aggregateRootId = aggregateRootId;
        this.aggregateRootTypeName = aggregateRootTypeName;
        this.version = version;
        this.domainEvents = domainEvents;
        for (IDomainEvent event : this.domainEvents) {
            if (event.getVersion() != this.version) {
                throw new InvalidVersionException(aggregateRootId, aggregateRootTypeName, version, event.getVersion());
            }
            event.setAggregateRootTypeName(this.aggregateRootTypeName);
        }
    }

    /**
     * 获取命令ID
     *
     * @return
     */
    public String getCommandId() {
        return commandId;
    }

    /**
     * 获取聚合根ID
     *
     * @return
     */
    public String getAggregateRootId() {
        return aggregateRootId;
    }

    /**
     * 获取聚合根类型名称
     *
     * @return
     */
    public String getAggregateRootTypeName() {
        return aggregateRootTypeName;
    }

    /**
     * 获取聚合根版本
     *
     * @return
     */
    public Long getVersion() {
        return version;
    }

    /**
     * 获取领域事件集合
     *
     * @return
     */
    public Collection<IDomainEvent> getDomainEvents() {
        return domainEvents;
    }
}
